package com.hq.util;

import com.hq.bean.Student;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//public class MyInterceptor extends HandlerInterceptorAdapter {
public class MyInterceptor implements HandlerInterceptor {

    // 请求前
    // 是否拦截
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        Student user =(Student) httpServletRequest.getSession().getAttribute("user");
        if(user !=null){
            return true; // 放行
        }else{
            httpServletResponse.sendRedirect("index.jsp"); //重定向
            return false; // 拦截
        }

    }

    //渲染前
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    // 请求后
    // 结束
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
